package com.safety.web.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.safety.web.entity.User;
import com.safety.web.service.UserService;
import com.safety.core.util.SMSTest;

@Controller
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;
    private SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    // 发送验证码
    @RequestMapping("/sendVerify")
    @ResponseBody
    public Map<String, Object> sendVerify(String phoneNum) {
        Map<String, Object> map = new LinkedHashMap<>();
        try {
            int a = (int) Math.round(Math.random() * 900000 + 100000);
            String content = "您好，您的验证码是" + a + "【平安好医生】";
            SMSTest.send(content, phoneNum);
            map.put("code", 200);
            map.put("verifyCode", content);
            map.put("message", "发送成功");
        } catch (Exception e) {
            map.put("code", 400);
            map.put("verification", e.getMessage());
            map.put("message", "发送失败,错误信息:"+e);
        }
        return map;
    }

    //判断是否注册
    @RequestMapping("/registerOrLogin")
    @ResponseBody
    public Map<String, Object> registerOrLogin(String phoneNum) {
        Map<String, Object> result = new LinkedHashMap<>();
        try {
            User user = userService.selectHasUser(phoneNum);
            result.put("status", 200);
            result.put("message", "登录成功");
            result.put("userId", user.getUserId());
            result.put("userPic", user.getUserPic());
            result.put("username", user.getUserName());
            result.put("age", user.getAge());
            result.put("sex", user.getSex());
            result.put("healthPoint", user.getHealthPoint());
        } catch (Exception e) {
            result.put("status", 201);
            result.put("message", "该用户未注册");
            result.put("userId", "");
            result.put("userPic", "");
            result.put("username", "");
            result.put("age", "");
            result.put("sex", "");
            result.put("healthPoint", "");
        }
        return result;
    }

    // 注册
    @RequestMapping("/register")
    @ResponseBody
    public Map<String, Object> register(String phoneNum, String userName, String sex) {
        Map<String, Object> result = new LinkedHashMap<>();
        User user = new User();
        try{
            User getUser = userService.selectHasUser(phoneNum);

            result.put("code", 400);
            result.put("message", "注册失败,该用户已注册");
            result.put("userId", getUser.getUserId());
            return result;
        }catch(Exception e ) {
                user.setPhoneNum(phoneNum);
                user.setUserName(userName);
                user.setSex(sex);
                user.setRegisterDate(df.format(new Date()));
                userService.insertRegister(user);

                User getNewUser = userService.selectHasUser(phoneNum);
                result.put("code", 200);
                result.put("message", "注册成功");
                result.put("userId", getNewUser.getUserId());
            return result;
        }
    }
//
//    // 修改头像
//    @RequestMapping("/updateUserPic")
//    @ResponseBody
//    public Map<String, Object> updateUserPic(User user,
//                                             int userId, String userPic) {
//        Map<String, Object> map = new LinkedHashMap<>();
//        try {
//            if (userPic.isEmpty() || "".equals(userPic) || "null".equals(userPic)) {
//                map.put("code", 400);
//                map.put("message", "上传图片为空,上传失败");
//            } else {
//                user.setUserId(userId);
//                user.setUserPic(userPic.replace(Data.URL.base, "").replace("upload/cache/", ""));// 改动.replace(Data.URL.base,"")
//                userService.updatemodifypicture(user);
//                map.put("code", 200);
//                map.put("message", "修改图片成功");
//            }
//
//
//        } catch (Exception e) {
//            map.put("code", 400);
//            map.put("message", "修改图片失败");
//        }
//        return map;
//    }
//
//
//    // 改性别
//    @RequestMapping("/changesex")
//    @ResponseBody
//    public Object changesex(User user, HttpServletRequest request)
//            throws IOException {
//        JSONObject json = JSONObject.fromObject(Public.getRequestBody(request));
//        JSONObject reqParam = new JSONObject();
//        try {
//            user.setPhoneNum(json.getString("phoneNum"));
//            user.setSex(json.getString("sex"));
//            userService.updatesex(user);
//            reqParam.put("message", "性别修改成功");
//            reqParam.put("code", 200);
//            return reqParam;
//        } catch (Exception e) {
//            reqParam.put("message", e.getMessage());
//            reqParam.put("code", 400);
//            return reqParam;
//        }
//    }
//
//    // 改昵称
//    @RequestMapping("/changeUserName")
//    @ResponseBody
//    public Object changeUserName(User user, HttpServletRequest request)
//            throws IOException {
//        JSONObject json = JSONObject.fromObject(Public.getRequestBody(request));
//        JSONObject reqParam = new JSONObject();
//        try {
//            user.setPhoneNum(json.getString("phoneNum"));
//            user.setPhoneNum(json.getString("userName"));
//            userService.updatereguser(user);
//            reqParam.put("message", "昵称修改成功");
//            reqParam.put("code", 200);
//            return reqParam;
//        } catch (Exception e) {
//            reqParam.put("message", e.getMessage());
//            reqParam.put("code", 400);
//            return reqParam;
//        }
//    }
//
//    // 改年龄
//    @RequestMapping("/changeage")
//    @ResponseBody
//    public Object changeage(User user, HttpServletRequest request)
//            throws IOException {
//        JSONObject json = JSONObject.fromObject(Public.getRequestBody(request));
//        JSONObject reqParam = new JSONObject();
//        try {
//            user.setPhoneNum(json.getString("phoneNum"));
//            user.setAge(json.getInt("age"));
//            userService.updateage(user);
//            reqParam.put("message", "年龄修改成功");
//            reqParam.put("code", 200);
//            return reqParam;
//        } catch (Exception e) {
//            reqParam.put("message", e.getMessage());
//            reqParam.put("code", 400);
//            return reqParam;
//        }
//    }

}
